<script type="text/javascript">
	$(document).ready(function(){

		$('#petition').dataTable();
		
		$("a[id^='info_']").click(function() {
			event.preventDefault();

			var numrow = $(this).attr("id");
            numrow = numrow.substr(5);

			$('<div>').dialog2({
				autoOpen: true,
				title: "ข้อมูลฎีกา",
				content: "dlgInfo_peti.php?petition_id=" + numrow,
				id: "modal-infoPeti"
			});

		});
	});

	function verifyPetition(id){
		var task = "verify";
		//alert(id);

			$.post('processPetition.php',{task: task, petition_id: id}, function(data) {
					window.location.href="index.php?pages=finance";
			});

	}

	function cancelPetition(id){
		var task = "cancel";
		//alert(id);

		$('<div>').dialog2({
			autoOpen: true,
			title: "ยกเลิกฎีกา",
			content: "dlgCancel_peti.php?petition_id=" + id,
			id: "modal-cancelPeti"
		});

	}

	function approvedCheque(id){
		var task = "approvedCheque";

		/*
		$.post('processPetition.php',{task: task, petition_id: id}, function(data) {
			window.location.href="index.php?pages=finance";
		});
		*/
		
		$('<div>').dialog2({
			autoOpen: true,
			title: "อนุมัติ",
			content: "dlgDate.php?petition_id=" + id + "&task=" + task,
			id: "modal-dlgDate"
		});


	}
	function disapprovalCheque(id){
		var task = "disapprovalCheque";

		$.post('processPetition.php',{task: task, petition_id: id}, function(data) {
			window.location.href="index.php?pages=cheque";
		});

	}

	function printCheque(id){
		var task = "printCheque";

		$('<div>').dialog2({
			autoOpen: true,
			title: "พิมพ์เช็ค",
			content: "dlgPrintCheque.php?petition_id=" + id,
			id: "modal-printCheque"
		});
	}

</script>

<?php
	//echo "finance.php"
	include("includes/connect.php");

	$db = new myDBS;

	//$where = "petition_id = '$petition_id'";
	$sql ="SELECT tbl_petition.*, pp_check_job.proID, projectName, amount, instalment FROM tbl_petition, pp_check_job WHERE tbl_petition.ppID = pp_check_job.ppID and approved_status = 'Approved' Order by approved_date asc";
	$result = $db->runSQL($sql);


?>
	<table id="petition" class="table table-striped" width="100%" >
		<caption><br></caption>
		<thead>
			<tr>
				<th>#</th>
				<th>เลขที่โครงการ</th>
				<th>ชื่อโครงการ</th>
				<th>งบประมาณ</th>
				<th>ยอดเบิกจ่าย</th>
				<th>งวดที่</th>
				<th>วันที่เบิกฏีกา</th>
				<th>รายละเอียดเพิ่มเติม</th>
				<th>พิมพ์เช็ค</th>
				<th>อนุมัติเช็ค</th>
			</tr>
		</thead>
		<tbody>
			<?php
				$i=0;
				while ( $rows = $db->fetch_object ( $result )){
					$i++;
			?>
			<tr>
				<td><?php echo $i; ?></td>
				<td>
				<?php 
						if($rows->veri_status == "Not Verify") {
				?>
				<a href="index.php?pages=sumPetition1&petition_id=<?php echo $rows->petition_id; ?>" target="_blank"><?php echo $rows->proID; ?></a>
				<?php
						} else {
							echo $rows->proID;
						}
				?>
					
				</td>
				<td><?php echo $rows->projectName; ?></td>
				<td><?php echo number_format($rows->amount); ?></td>
				<td><?php echo number_format($rows->inpNet); ?></td>
				<td><?php echo $rows->instalment; ?></td>
				<td><?php echo $rows->date_create; ?></td>
				<td>
					<a href="#" id="info_<?php echo $rows->ppID; ?>"><i class="icon-info-sign"></i>&nbsp;ดู</a>
				</td>
	 			<td>
					<a href="javascript:printCheque('<?php echo $rows->petition_id; ?>')" data-toggle="tooltip" title="พิมพ์เช็ค"><i class="icon-print"></i></a>
				</td>
				<td>
					<?php 
					
						if($rows->cheque_status <> "Not Approved") {
							echo $rows->cheque_status;
						} else {
					?>
						<a href="javascript:approvedCheque('<?php echo $rows->petition_id; ?>')" data-toggle="tooltip" title="อนุมัติ"><i class="icon-thumbs-up"></i></a>&nbsp;<big>|</big>&nbsp;<a href="javascript:disapprovalCheque('<?php echo $rows->petition_id; ?>')" data-toggle="tooltip" title="ไม่อนุมัติ"><i class="icon-thumbs-down"></i></a>

						
					<?php 
						}
					
					?>
				</td>
			</tr>
			<?php
					
				}
			?>

		</tbody>


	</table>
